package com.wy.authority.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wy.authority.entity.SysUser;
import com.wy.authority.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author 忘忧
 * @Date Created in 2021/1/12 14:28
 * @Description:
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

  //分页
    @ResponseBody
    @GetMapping("/findall")
    public PageInfo<SysUser> findAll(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "6") Integer size){
        PageHelper.startPage(page,size);
        List<SysUser> list=userService.find();
        PageInfo<SysUser> pageInfo =new PageInfo<>(list);
        return pageInfo;
    }

    //账号登录
    @PostMapping("/login")
    @ResponseBody
    public String login(@RequestBody SysUser sysUser) {
        SysUser user1 = userService.login(sysUser);
        String code1 = null;
        if (user1 != null) {
            String code =user1.getPost_code();
            if(code.equals("shopper")){
                 code1="dept";
            }else if(code.equals("dept")){
                 code1="definancept";
            }
            else if(code.equals("finance")){
                 code1="presidents";
            }
            else if(code.equals("presidents")){
                 code1="president";
            }else if(code.equals("president")){
                code1="president";
            }else if(code.equals("admin")){
                code1="admin";
            }
            String phonenumber=user1.getPhonenumber();
            redisTemplate.opsForValue().set("code1",code1);
            redisTemplate.opsForValue().set("phonenumber",phonenumber);
            redisTemplate.opsForValue().set("code",code);
            return "success";
        }
        return "false";
    }

}

